package com.dd.cloud.user.service.product.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.UserConstants;
import com.dd.cloud.common.enums.ZkExceptionEnum;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.interceptor.UserInterceptor;
import com.dd.cloud.common.utils.ValidatorUtils;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.doctor.DoctorPresenterNum;
import com.dd.cloud.user.entity.doctor.DoctorUser;
import com.dd.cloud.user.entity.product.ProductGiveRecord;
import com.dd.cloud.user.entity.product.ProductGiveRule;
import com.dd.cloud.user.mapper.product.ProductGiveRuleMapper;
import com.dd.cloud.user.req.product.ProductGiveRuleSystemPageReq;
import com.dd.cloud.user.req.product.ProductGiveRuleSystemUpdateReq;
import com.dd.cloud.user.res.product.ProductGiveRuleSystemPageRes;
import com.dd.cloud.user.service.doctor.IDoctorUserService;
import com.dd.cloud.user.service.product.IProductGiveRuleService;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 * @date 2024/1/23
 */
@Slf4j
@Service
public class ProductGiveRuleServiceImpl extends ServiceImpl<ProductGiveRuleMapper, ProductGiveRule> implements IProductGiveRuleService {

    @Autowired
    private IDoctorUserService doctorUserService;

    /**
     * 查询收费服务产品赠送规则
     *
     * @param req
     * @return
     */
    @Override
    public IPage<ProductGiveRuleSystemPageRes> queryProductGiveRuleSystemPage(ProductGiveRuleSystemPageReq req) {
        IPage<ProductGiveRuleSystemPageRes> iPage = req.pagePojo();
        iPage = getBaseMapper().queryProductGiveRuleSystemPage(iPage, req);
        return iPage;
    }

    /**
     * 批量修改收费服务产品赠送规则
     *
     * @param reqs
     */
    @Override
    public void updateBatchProductGiveRuleSystem(List<ProductGiveRuleSystemUpdateReq> reqs) {
        LoginInfo loginInfo = UserInterceptor.getLoginUserInfo(LoginInfo.UserType.ADMIN).get();
        List<ProductGiveRule> list = new ArrayList<>(reqs.size());
        for (ProductGiveRuleSystemUpdateReq item : reqs) {
            ProductGiveRule temp = getById(item.getId());
            if (temp == null) {
                throw new ZekeException(400, "id:" + item.getId() + "不存在");
            }
            if (temp.getAccountType() == 1) {
                if (temp.getAccountAuth() == 1) {
                    ValidatorUtils.verifyGroup(item, item.getClass(), ProductGiveRuleSystemUpdateReq.Valid1.class);
                } else {
                    ValidatorUtils.verifyGroup(item, item.getClass(), ProductGiveRuleSystemUpdateReq.Valid2.class);
                }
            } else if (temp.getAccountType() == 2) {
                if (temp.getAccountAuth() == 2) {
                    ValidatorUtils.verifyGroup(item, item.getClass(), ProductGiveRuleSystemUpdateReq.Valid3.class);
                }
            }
            ProductGiveRule productGiveRule = new ProductGiveRule();
            BeanUtils.copyProperties(item, productGiveRule);
            productGiveRule.setUpdateId(loginInfo.getId());
            productGiveRule.setUpdateType(loginInfo.getType());
            productGiveRule.setUpdateDate(LocalDateTime.now());
            list.add(productGiveRule);
        }
        boolean flag = updateBatchById(list);
        if (!flag) {
            throw new ZekeException(ZkExceptionEnum.UPDATE_ERROR);
        }
    }


}
